using System;
using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Logic;
using FireStarterModeller.BO;
using FireStarterModeller.Test;
using Habanero.Base.Exceptions;
using NUnit.Framework;

namespace Firestarter.DB_DomainModel.Logic.Test
{
    [TestFixture]
    public class TestDBMySQLPostProcessor : TestBase
    {
 [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DBMySqlPostProcessor dbMySqlToDMClassImporter = new DBMySqlPostProcessor();
            //---------------Test Result -----------------------
            Assert.IsNotNull(dbMySqlToDMClassImporter);
        }

        [Test]
        public void Test_ProcessColumn_WhenIsChar38_ShouldChangeToGuid()
        {
            //---------------Set up test pack-------------------
            IDBPostProcessor dbPostProcessor = new DBMySqlPostProcessor();
            var dbColumn = new DBColumn(GetRandomString()) {DataType = DBDataTypes.CHAR, DataLength = 38};

            //---------------Assert Precondition----------------
            Assert.AreEqual(DBDataTypes.CHAR, dbColumn.DataType);
            Assert.AreEqual(38, dbColumn.DataLength);
            //---------------Execute Test ----------------------
            dbPostProcessor.ProcessColumn(dbColumn);
            //---------------Test Result -----------------------
            Assert.AreEqual(DBDataTypes.UNIQUEIDENTIFIER, dbColumn.DataType);
            Assert.AreEqual(0, dbColumn.DataLength);
        }
        [Test]
        public void Test_ProcessColumn_WhenIsChar15_ShouldNotChange()
        {
            //---------------Set up test pack-------------------
            IDBPostProcessor dbPostProcessor = new DBMySqlPostProcessor();
            var dbColumn = new DBColumn(GetRandomString()) {DataType = DBDataTypes.CHAR, DataLength = 15};

            //---------------Assert Precondition----------------
            Assert.AreEqual(DBDataTypes.CHAR, dbColumn.DataType);
            Assert.AreEqual(15, dbColumn.DataLength);
            //---------------Execute Test ----------------------
            dbPostProcessor.ProcessColumn(dbColumn);
            //---------------Test Result -----------------------
            Assert.AreEqual(DBDataTypes.CHAR, dbColumn.DataType);
            Assert.AreEqual(15, dbColumn.DataLength);
        }

        [Test]
        public void Test_Process_WhenNull_ShouldNotErrorOut()
        {
            //---------------Set up test pack-------------------
            IDBPostProcessor dbPostProcessor = new DBMySqlPostProcessor();
            //---------------Assert Precondition----------------
 
            //---------------Execute Test ----------------------
            dbPostProcessor.Process(null);
            //---------------Test Result -----------------------
            Assert.IsTrue(true, "If got here then OK");
        }
        [Test]
        public void Test_Process_WhenIsString38_ShouldChangeToGuid()
        {
            //---------------Set up test pack-------------------
            IDBPostProcessor dbPostProcessor = new DBMySqlPostProcessor();
            IDBDatabase database = new DBDatabase();
            var dbColumn = new DBColumn(GetRandomString());
            var table = new DBTable(GetRandomString());
            table.Columns.Add(dbColumn);
            database.Tables.Add(table);
            dbColumn.DataType = DBDataTypes.CHAR;
            dbColumn.DataLength = 38;

            //---------------Assert Precondition----------------
            Assert.AreEqual(DBDataTypes.CHAR, dbColumn.DataType);
            Assert.AreEqual(38, dbColumn.DataLength);
            //---------------Execute Test ----------------------
            dbPostProcessor.Process(database);
            //---------------Test Result -----------------------
            Assert.AreEqual(DBDataTypes.UNIQUEIDENTIFIER, dbColumn.DataType);
        }

        [Test]
        public void Test_Process_WhenTwoProps_AndIsString38_ShouldChangeIDToGuid()
        {
            //---------------Set up test pack-------------------
            IDBPostProcessor dbPostProcessor = new DBMySqlPostProcessor();
            IDBDatabase database = new DBDatabase();
            var dbColumn = new DBColumn(GetRandomString()){DataType = DBDataTypes.CHAR, DataLength = 20};
            var idColumn = new DBColumn(GetRandomString()) {DataType = DBDataTypes.CHAR, DataLength = 38};
            var table = new DBTable(GetRandomString());
            table.Columns.Add(dbColumn);
            table.Columns.Add(idColumn);
            database.Tables.Add(table);

            //---------------Assert Precondition----------------
            Assert.AreEqual(2, table.Columns.Count);
            Assert.AreEqual(DBDataTypes.CHAR, idColumn.DataType);
            Assert.AreEqual(38, idColumn.DataLength);
            //---------------Execute Test ----------------------
            dbPostProcessor.Process(database);
            //---------------Test Result -----------------------
            Assert.AreEqual(DBDataTypes.UNIQUEIDENTIFIER, idColumn.DataType);
            Assert.AreEqual(0, idColumn.DataLength);
            Assert.AreEqual(DBDataTypes.CHAR, dbColumn.DataType);
            Assert.AreEqual(20, dbColumn.DataLength);
        }
        [Test]
        public void Test_Process_WhenTwoTables_AndTwoProps_AndIsString38_ShouldChangeIDToGuid()
        {
            //---------------Set up test pack-------------------
            IDBPostProcessor dbPostProcessor = new DBMySqlPostProcessor();
            IDBDatabase database = new DBDatabase();
            var dbColumn = new DBColumn(GetRandomString()){DataType = DBDataTypes.CHAR, DataLength = 20};
            var idColumn = new DBColumn(GetRandomString()) {DataType = DBDataTypes.CHAR, DataLength = 38};
            var table = new DBTable(GetRandomString());
            table.Columns.Add(dbColumn);
            table.Columns.Add(idColumn);
            database.Tables.CreateBusinessObject();
            database.Tables.Add(table);

            //---------------Assert Precondition----------------
            Assert.AreEqual(2, table.Columns.Count);
            Assert.AreEqual(DBDataTypes.CHAR, idColumn.DataType);
            Assert.AreEqual(38, idColumn.DataLength);
            //---------------Execute Test ----------------------
            dbPostProcessor.Process(database);
            //---------------Test Result -----------------------
            Assert.AreEqual(DBDataTypes.UNIQUEIDENTIFIER, idColumn.DataType);
            Assert.AreEqual(0, idColumn.DataLength);
            Assert.AreEqual(DBDataTypes.CHAR, dbColumn.DataType);
            Assert.AreEqual(20, dbColumn.DataLength);
        }

        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }
    }
}